package cn.wjdiankong.levideo.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.wjdiankong.levideo.R;

/**
 * Created by i on 2015/1/20.
 */
public class UIUtil {
	
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static Bitmap zoomImg(Bitmap bm, int newWidth ,int newHeight){
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }

    @SuppressLint("NewApi")
	public static void setListViewOverScrollMode(AbsListView listView){
        if(listView != null){
            listView.setOverScrollMode(AbsListView.OVER_SCROLL_NEVER);
        }
    }

    /**
	 * 获取屏幕的宽度
	 * 
	 * @param context
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static int getScreenWidth(Context context) {
		WindowManager manager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = manager.getDefaultDisplay();
		return display.getWidth();
	}

	/**
	 * 获取屏幕的高度
	 * 
	 * @param context
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static int getScreenHeight(Context context) {
		WindowManager manager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = manager.getDefaultDisplay();
		return display.getHeight();
	}

	/**
	 * 获取屏幕中控件顶部位置的高度--即控件顶部的Y点
	 * 
	 * @return
	 */
	public static int getScreenViewTopHeight(View view) {
		return view.getTop();
	}

	/**
	 * 获取屏幕中控件底部位置的高度--即控件底部的Y点
	 * 
	 * @return
	 */
	public static int getScreenViewBottomHeight(View view) {
		return view.getBottom();
	}

	/**
	 * 获取屏幕中控件左侧的位置--即控件左侧的X点
	 * 
	 * @return
	 */
	public static int getScreenViewLeftHeight(View view) {
		return view.getLeft();
	}

	/**
	 * 获取屏幕中控件右侧的位置--即控件右侧的X点
	 * 
	 * @return
	 */
	public static int getScreenViewRightHeight(View view) {
		return view.getRight();
	}
	
	@SuppressLint("InflateParams")
	public static void showLongHintToast(Context ctx,String str){
		View view = LayoutInflater.from(ctx).inflate(R.layout.toast_top_layout, null);
		TextView msgText = (TextView)view.findViewById(R.id.msg_text);
		msgText.setText(str);
        Toast toast = new Toast(ctx);
        toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.TOP, 0,dip2px(ctx, 85));
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
	}
	
	@SuppressLint("InflateParams")
	public static void showShortHintToast(Context ctx,String str){
		View view = LayoutInflater.from(ctx).inflate(R.layout.toast_bottom_layout, null);
		TextView msgText = (TextView)view.findViewById(R.id.msg_text);
		msgText.setText(str);
        Toast toast = new Toast(ctx);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
	}
	
	public static Bitmap drawableToBitmap(Drawable drawable) {  
		try{
			Rect r = drawable.getBounds();
			Bitmap bitmap = Bitmap  
					.createBitmap(  
							r.right,  
							r.bottom,  
							drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
									: Bitmap.Config.RGB_565);
			Canvas canvas = new Canvas(bitmap);  
			drawable.setBounds(0, 0, r.right, r.bottom);  
			drawable.draw(canvas);  
			return bitmap;  
		}catch(Exception e){
			return null;
		}
	} 
	
	private static byte[] getByteArrays(InputStream openInputStream) {
        ByteArrayOutputStream out;
        try {
            out = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024];
            int read;
            while ((read = openInputStream.read(bytes, 0, 1024)) != -1) {
                out.write(bytes, 0, read);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return out.toByteArray();
    }
	
	public static Bitmap getResizedBitmap(InputStream openInputStream, int limitWith) {
        byte[] bytes = getByteArrays(openInputStream);
        try {
            if (bytes != null) {
                BitmapFactory.Options opt = new BitmapFactory.Options();
                int sample = 1;
                opt.inJustDecodeBounds = true;
                opt.inSampleSize = sample;
                BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opt);
                while ((opt.outWidth < opt.outHeight ? opt.outWidth : opt.outHeight) > limitWith) {
                    opt.inSampleSize = ++sample;
                    opt.inJustDecodeBounds = true;
                    BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opt);
                }
                opt.inSampleSize = sample;
                opt.inJustDecodeBounds = false;
                Bitmap map = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opt);
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
	
}
